package dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import entity.Association;
import entity.Union;

@Stateless
public class AssociationDao {
	@PersistenceContext
	EntityManager manager;
	
	
	
	//finds all the associations
	public List<Association> allAssociations(){
		String spql="Select a from Association a";
		TypedQuery<Association> query=manager.createQuery(spql, Association.class);
		List<Association> allAssociationList=query.getResultList();
		return allAssociationList;
	}
	
	//finds the association of an employee
	public List<Association> findEmpAssociation(int employeeID){
		String spql="Select a from Association a where employee_Id='" + employeeID + "'";
		TypedQuery<Association> query=manager.createQuery(spql, Association.class);
		List<Association> empAssociation=query.getResultList();
		return empAssociation;
	}

	//finds only one association
	public Association findAssociation(int id){
		Association association=manager.find(Association.class, id);
		return association;
	}
	
	//deletes an association
	public int deleteAssociation(Association association){
		try {
			Association deleted=manager.merge(association);
			manager.remove(deleted);
		}
		catch(Exception e){
			return -1;
		}
		return 0;
	}
	public void updateAssociation(Association association){
		manager.merge(association);
	}
	public String registerAssociation(Association association){
		manager.persist(association);
		return null;
	}
}
